const qs = Qs;
axios.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
});

/**
 * 发送请求前统一处理，设置token等
 */
axios.interceptors.request.use(config => {
  console.log(config);
  return config
}, error => {
  return Promise.reject(error)
});


/**
 * 返回结果统一处理
 */
axios.interceptors.response.use(data => {
  console.log(data);
  return data;
}, error => {
  console.log(error);
  return Promise.reject(error);
});


/**
 * 同时发送多个请求
 * @param fnArr
 * @returns {Promise<any>}
 */
function requestAll(fnArr) {
  return new Promise((resolve, reject) => {
    axios.all(fnArr)
      .then(axios.spread(function() {
        resolve(arguments)
      }))
  })
}

/**
 * 发送请求
 */
function requestServe(apiUrl = '', params, config = {}) { // config: {type: 'post'}
  const baseUrl = 'http://221.1.221.179:19201';
  params = params || {};
  let type = config.type || 'post';
  let url = baseUrl + apiUrl;
  return new Promise((resolve, reject) => {
    let httpParam = qs.stringify(params);
    if (type !== 'post') {
      httpParam = {
        params
      }
    }
    axios[type](url, httpParam).then(function(response) {
      if (response) {
        resolve(response.data)
      }
    }).catch(function(err) {
      reject(err)
    })
  })
}
